<template>
  <el-card>
    <!-- 面包屑 -->
    <div slot="header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">商品分类</a></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 筛选 -->
    <div class="filter-bar">
      <div class="l-form"></div>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="small"
        @click="cateShow = true"
        >添加</el-button
      >
    </div>

    <!-- 表格区域 -->
    <div class="tab-list">
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        border
      >
        <template slot="canuse" slot-scope="scope">
          <i class="el-icon-error" v-if="scope.row.cat_deleted"></i>
          <i class="el-icon-success" v-else></i>
        </template>
        <template slot="level" slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="primary" v-if="scope.row.cat_level === 2">三级</el-tag>
        </template>
        <template slot="action">
          <el-button type="success" size="small">编辑</el-button>
          <el-button type="primary" size="small">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="sizeChange"
        @current-change="pageChange"
        :current-page="pagenum"
        :page-sizes="[1, 2, 3, 4, 5, 6]"
        :page-size="pagesize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 添加弹框 -->
    <el-dialog title="商品分类" :visible.sync="cateShow" width="30%">
      <el-form
        :model="cateForm"
        :rules="rules"
        ref="cateForm"
        label-width="100px"
        size="normal"
      >
        <el-form-item label="分类名" prop="cat_name">
          <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="上级分类">
          <el-cascader
            v-model="cateForm.cat_pid"
            :options="cateAllList"
            clearable
            :props="{
              checkStrictly: true,
              value: 'cat_id',
              label: 'cat_name'
            }"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cateShow = false">取 消</el-button>
        <el-button type="primary" @click="cateSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
let defaultItem = {
  cat_pid: 0,
  cat_name: "",
  cat_level: 0
};
export default {
  data() {
    return {
      cateList: [], //当前页数的商品列表
      cateAllList: [], //获取所有列表
      pagenum: 1,
      pagesize: 4,
      total: 0,
      cateShow: false,
      columns: [
        { label: "分类名称", prop: "cat_name" },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "canuse"
        },
        {
          label: "层级",
          prop: "cat_level",
          type: "template",
          template: "level"
        },
        { label: "操作", type: "template", template: "action" }
      ],
      cateForm: {
        ...defaultItem
      },
      rules: {
        cat_name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getCateList();
    this.getCateAllList();
  },
  methods: {
    async getCateList() {
      let { data: res } = await this.$axios.get("categories", {
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.cateList = res.data.result;
      this.total = res.data.total;
      console.log(res);
    },
    // 获取所有列表
    async getCateAllList() {
      let { data: res } = await this.$axios.get("categories");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.cateAllList = res.data;
    },
    sizeChange(val) {
      this.pagesize = val;
      this.getCateList();
    },
    pageChange(val) {
      this.pagenum = val;
      this.getCateList();
    },
    // 添加分类
    cateSubmit() {
      this.$refs.cateForm.validate(async valid => {
        if (!valid) return;
        //    判断是数组还是数值cat_pid[]/0
        if (Array.isArray(this.cateForm.cat_pid)) {
          if (this.cateForm.cat_pid.length) {
            this.cateForm.cat_level = this.cateForm.cat_pid.length;
            this.cateForm.cat_pid = this.cateForm.cat_pid[
              this.cateForm.cat_pid.length - 1
            ];
          } else {
            this.cateForm.cat_pid = 0;
            this.cateForm.cat_level = 0;
          }
        }
        let { data: res } = await this.$axios.post("categories", this.cateForm);
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg);
        this.cateForm = {
          ...defaultItem
        };
        this.$message.success("添加分类成功");
        this.cateShow = false;
        this.getCateList();
      });
    }
  }
};
</script>

<style lang="less" scoped>
</style>